package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.pojo.Organization;
import com.zoshine.zzg.pojo.pojostatus.CommonStatus;
import com.zoshine.zzg.pojo.pojostatus.OrgType;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织Controller
 *
 * @author 田沛凌
 * @time 2017年6月22日 下午1:11:31
 */
@RestController
@RequestMapping(value = "/org", method = RequestMethod.POST)
public class OrganizationController extends BaseController{
    private OrganizationService organizationService;

    @Autowired
    public void setOrganizationService(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    /**
     * 增加组织
     *
     * @param organization 组织信息
     */
    @RequestMapping("/addOrg")
    public BaseResponse addOrg(Organization organization) {
        organizationService.add(organization);
        return AppResponse.OK;
    }

    /**
     * 根据ID删除组织
     *
     * @param id 组织编号
     */
    @RequestMapping("/delOrg")
    public BaseResponse delOrg(Long id) {
        organizationService.del(id);
        return AppResponse.OK;
    }

    /**
     * 修改组织
     *
     * @param organization 组织信息
     */
    @RequestMapping("/editOrg")
    public BaseResponse editOrg(Organization organization) {
        organizationService.edit(organization);
        return AppResponse.OK;
    }

    /**
     * 获取组织信息
     *
     * @param id 组织编号
     */
    @RequestMapping("/getOrg")
    public BaseResponse getOrg(Long id) {
        Organization organization = organizationService.get(id);
        return new AppResponse(organization);
    }

    /**
     * 获取组织分页列表
     *
     * @param organization 查询参数
     * @return 组织分页列表
     */
    @RequestMapping("/pageOrg")
    public BaseResponse pageOrg(Organization organization) {
        List<Organization> orgList = organizationService.list(organization);
        return new AppResponse(new PageInfo<>(orgList));
    }


    /**
     * 获取组织列表
     *
     * @param organization 查询参数
     * @return 组织列表
     */
    @RequestMapping("/listOrg")
    public BaseResponse listOrg(Organization organization) {
        List<Organization> orgList = organizationService.list(organization);
        return new AppResponse(orgList);
    }

    /**
     * 获取监所、看守所列表(加权限)
     */
    @RequestMapping("/listPrison")
    public BaseResponse listPrison() {
        Organization organization = new Organization();
        List<Long> prisonPower = super.getPrisonPower();
        organization.setPrisonPower(prisonPower);
        organization.setOrgType(OrgType.Prison.getValue());
        List<Organization> list = organizationService.list(organization);
        return new AppResponse(list);
    }

    /**
     * 获取供应商列表(加权限)
     */
    @RequestMapping("listSupplier")
    public BaseResponse listSupplier() {
        Organization organization = new Organization();
        List<Long> supplierPower = super.getSupplierPower();
        organization.setPrisonPower(supplierPower);
        organization.setOrgType(OrgType.Supplier.getValue());
        List<Organization> list = organizationService.list(organization);
        return new AppResponse(list);
    }

    /**
     * 获取组织类型列表
     */
    @RequestMapping("/listOrgType")
    public BaseResponse listOrgType() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrgType orgType : OrgType.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", orgType.getValue());
            map.put("text", OrgType.getDesc(orgType.getValue()));
            list.add(map);
        }
        return new AppResponse(list);
    }

    /**
     * 获取组织状态列表
     */
    @RequestMapping("/listOrgStatus")
    public BaseResponse listOrgStatus() {
        return new AppResponse(CommonStatus.getList());
    }

    /**
     *获取监所列表(不带权限)
     * @return 监所列表
     */
    @RequestMapping("listPrisonRelation")
    public BaseResponse listPrisonRelation(){
        Organization organization = new Organization();
        organization.setOrgType(OrgType.Prison.getValue());
        List<Organization> list = organizationService.list(organization);
        return new AppResponse(list);
    }

    /**
     * 获取供应商列表(不带权限)
     * @return 供应商列表
     */
    @RequestMapping("listSupplierRelation")
    public BaseResponse listSupplierRelation(){
        Organization organization = new Organization();
        organization.setOrgType(OrgType.Supplier.getValue());
        List<Organization> list = organizationService.list(organization);
        return new AppResponse(list);
    }
}
